package com.bjyhl.medical.test.recycler;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bjyhl.medical.R;

import java.util.ArrayList;
import java.util.List;

public class SimplePubuAdapter extends RecyclerView.Adapter<SimplePubuAdapter.PubuViewHolder> {

    private LayoutInflater mLayoutInflater;
    private Context mContext;
    private List<String> mDatas;
    private List<Integer> mHeights;
    private IOnItemClickListener mItemClickListener;

    public void setOnItemClickListener(IOnItemClickListener listener) {
        this.mItemClickListener = listener;
    }

    public SimplePubuAdapter(Context context, List<String> datas) {
        this.mContext = context;
        this.mDatas = datas;
        mLayoutInflater = LayoutInflater.from(context);
        mHeights = new ArrayList<Integer>();
        for(int i=0; i<mDatas.size(); i++){
            mHeights.add((int)(200 + Math.random()*200));
        }
    }

//    @Override
//    public int getItemViewType(int position) {
//        return super.getItemViewType(position);
//    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    public void addItem(int pos) {
        String str = "@" + (char)('A' + pos);
        mDatas.add(pos, str);
        notifyItemInserted(pos);
    }

    public void removeItem(int pos) {
        mDatas.remove(pos);
        notifyItemRemoved(pos);
    }

    @Override
    public void onBindViewHolder(@NonNull final PubuViewHolder holder, final int pos) {
        ViewGroup.LayoutParams lp = holder.itemView.getLayoutParams();
        lp.height = mHeights.get(pos);
        holder.itemView.setLayoutParams(lp);
        holder.tv.setText(mDatas.get(pos));

        if(this.mItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mItemClickListener.onItemClick(holder.itemView, holder.getLayoutPosition());
                }
            });
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    mItemClickListener.onItemLongClick(holder.itemView, holder.getLayoutPosition());
                    return true;
                }
            });
        }
    }

  @NonNull
    @Override
    public PubuViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = mLayoutInflater.inflate(R.layout.item_test_simple, viewGroup, false);
        PubuViewHolder myViewHolder = new PubuViewHolder(view);
        return myViewHolder;
    }

    class PubuViewHolder extends RecyclerView.ViewHolder {
        TextView tv;
        public PubuViewHolder(View arg0) {
            super(arg0);
            tv = arg0.findViewById(R.id.tv_title);
        }
    }

    public interface IOnItemClickListener {
        void onItemClick(View view, int pos);
        void onItemLongClick(View view, int post);
    }
}

